/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package wggcth.persistencia.controle;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import wggcth.persistencia.controle.exceptions.IllegalOrphanException;
import wggcth.persistencia.controle.exceptions.NonexistentEntityException;
import wggcth.persistencia.vo.Experimento;
import wggcth.persistencia.vo.Usuario;
import wggcth.persistencia.vo.Amostra;
import java.util.ArrayList;
import java.util.Collection;
import wggcth.persistencia.vo.DadosExperimento;
import wggcth.persistencia.vo.Variavel;

/**
 *
 * @author deusemar
 */
public class ExperimentoJpaController {

    public ExperimentoJpaController() {
        emf = Persistence.createEntityManagerFactory("wggcthPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Experimento experimento) {
        if (experimento.getAmostraCollection() == null) {
            experimento.setAmostraCollection(new ArrayList<Amostra>());
        }
        if (experimento.getDadosExperimentoCollection() == null) {
            experimento.setDadosExperimentoCollection(new ArrayList<DadosExperimento>());
        }
        if (experimento.getVariavelCollection() == null) {
            experimento.setVariavelCollection(new ArrayList<Variavel>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Usuario idusuario = experimento.getIdusuario();
            if (idusuario != null) {
                idusuario = em.getReference(idusuario.getClass(), idusuario.getIdusuario());
                experimento.setIdusuario(idusuario);
            }
            Collection<Amostra> attachedAmostraCollection = new ArrayList<Amostra>();
            for (Amostra amostraCollectionAmostraToAttach : experimento.getAmostraCollection()) {
                amostraCollectionAmostraToAttach = em.getReference(amostraCollectionAmostraToAttach.getClass(), amostraCollectionAmostraToAttach.getIdamostra());
                attachedAmostraCollection.add(amostraCollectionAmostraToAttach);
            }
            experimento.setAmostraCollection(attachedAmostraCollection);
            Collection<DadosExperimento> attachedDadosExperimentoCollection = new ArrayList<DadosExperimento>();
            for (DadosExperimento dadosExperimentoCollectionDadosExperimentoToAttach : experimento.getDadosExperimentoCollection()) {
                dadosExperimentoCollectionDadosExperimentoToAttach = em.getReference(dadosExperimentoCollectionDadosExperimentoToAttach.getClass(), dadosExperimentoCollectionDadosExperimentoToAttach.getDadosExperimentoPK());
                attachedDadosExperimentoCollection.add(dadosExperimentoCollectionDadosExperimentoToAttach);
            }
            experimento.setDadosExperimentoCollection(attachedDadosExperimentoCollection);
            Collection<Variavel> attachedVariavelCollection = new ArrayList<Variavel>();
            for (Variavel variavelCollectionVariavelToAttach : experimento.getVariavelCollection()) {
                variavelCollectionVariavelToAttach = em.getReference(variavelCollectionVariavelToAttach.getClass(), variavelCollectionVariavelToAttach.getIdvariavel());
                attachedVariavelCollection.add(variavelCollectionVariavelToAttach);
            }
            experimento.setVariavelCollection(attachedVariavelCollection);
            em.persist(experimento);
            if (idusuario != null) {
                idusuario.getExperimentoCollection().add(experimento);
                idusuario = em.merge(idusuario);
            }
            for (Amostra amostraCollectionAmostra : experimento.getAmostraCollection()) {
                Experimento oldIdexperimentoOfAmostraCollectionAmostra = amostraCollectionAmostra.getIdexperimento();
                amostraCollectionAmostra.setIdexperimento(experimento);
                amostraCollectionAmostra = em.merge(amostraCollectionAmostra);
                if (oldIdexperimentoOfAmostraCollectionAmostra != null) {
                    oldIdexperimentoOfAmostraCollectionAmostra.getAmostraCollection().remove(amostraCollectionAmostra);
                    oldIdexperimentoOfAmostraCollectionAmostra = em.merge(oldIdexperimentoOfAmostraCollectionAmostra);
                }
            }
            for (DadosExperimento dadosExperimentoCollectionDadosExperimento : experimento.getDadosExperimentoCollection()) {
                Experimento oldExperimentoOfDadosExperimentoCollectionDadosExperimento = dadosExperimentoCollectionDadosExperimento.getExperimento();
                dadosExperimentoCollectionDadosExperimento.setExperimento(experimento);
                dadosExperimentoCollectionDadosExperimento = em.merge(dadosExperimentoCollectionDadosExperimento);
                if (oldExperimentoOfDadosExperimentoCollectionDadosExperimento != null) {
                    oldExperimentoOfDadosExperimentoCollectionDadosExperimento.getDadosExperimentoCollection().remove(dadosExperimentoCollectionDadosExperimento);
                    oldExperimentoOfDadosExperimentoCollectionDadosExperimento = em.merge(oldExperimentoOfDadosExperimentoCollectionDadosExperimento);
                }
            }
            for (Variavel variavelCollectionVariavel : experimento.getVariavelCollection()) {
                Experimento oldIdexperimentoOfVariavelCollectionVariavel = variavelCollectionVariavel.getIdexperimento();
                variavelCollectionVariavel.setIdexperimento(experimento);
                variavelCollectionVariavel = em.merge(variavelCollectionVariavel);
                if (oldIdexperimentoOfVariavelCollectionVariavel != null) {
                    oldIdexperimentoOfVariavelCollectionVariavel.getVariavelCollection().remove(variavelCollectionVariavel);
                    oldIdexperimentoOfVariavelCollectionVariavel = em.merge(oldIdexperimentoOfVariavelCollectionVariavel);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Experimento experimento) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Experimento persistentExperimento = em.find(Experimento.class, experimento.getIdexperimento());
            Usuario idusuarioOld = persistentExperimento.getIdusuario();
            Usuario idusuarioNew = experimento.getIdusuario();
            Collection<Amostra> amostraCollectionOld = persistentExperimento.getAmostraCollection();
            Collection<Amostra> amostraCollectionNew = experimento.getAmostraCollection();
            Collection<DadosExperimento> dadosExperimentoCollectionOld = persistentExperimento.getDadosExperimentoCollection();
            Collection<DadosExperimento> dadosExperimentoCollectionNew = experimento.getDadosExperimentoCollection();
            Collection<Variavel> variavelCollectionOld = persistentExperimento.getVariavelCollection();
            Collection<Variavel> variavelCollectionNew = experimento.getVariavelCollection();
            List<String> illegalOrphanMessages = null;
            for (Amostra amostraCollectionOldAmostra : amostraCollectionOld) {
                if (!amostraCollectionNew.contains(amostraCollectionOldAmostra)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Amostra " + amostraCollectionOldAmostra + " since its idexperimento field is not nullable.");
                }
            }
            for (DadosExperimento dadosExperimentoCollectionOldDadosExperimento : dadosExperimentoCollectionOld) {
                if (!dadosExperimentoCollectionNew.contains(dadosExperimentoCollectionOldDadosExperimento)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain DadosExperimento " + dadosExperimentoCollectionOldDadosExperimento + " since its experimento field is not nullable.");
                }
            }
            for (Variavel variavelCollectionOldVariavel : variavelCollectionOld) {
                if (!variavelCollectionNew.contains(variavelCollectionOldVariavel)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Variavel " + variavelCollectionOldVariavel + " since its idexperimento field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (idusuarioNew != null) {
                idusuarioNew = em.getReference(idusuarioNew.getClass(), idusuarioNew.getIdusuario());
                experimento.setIdusuario(idusuarioNew);
            }
            Collection<Amostra> attachedAmostraCollectionNew = new ArrayList<Amostra>();
            for (Amostra amostraCollectionNewAmostraToAttach : amostraCollectionNew) {
                amostraCollectionNewAmostraToAttach = em.getReference(amostraCollectionNewAmostraToAttach.getClass(), amostraCollectionNewAmostraToAttach.getIdamostra());
                attachedAmostraCollectionNew.add(amostraCollectionNewAmostraToAttach);
            }
            amostraCollectionNew = attachedAmostraCollectionNew;
            experimento.setAmostraCollection(amostraCollectionNew);
            Collection<DadosExperimento> attachedDadosExperimentoCollectionNew = new ArrayList<DadosExperimento>();
            for (DadosExperimento dadosExperimentoCollectionNewDadosExperimentoToAttach : dadosExperimentoCollectionNew) {
                dadosExperimentoCollectionNewDadosExperimentoToAttach = em.getReference(dadosExperimentoCollectionNewDadosExperimentoToAttach.getClass(), dadosExperimentoCollectionNewDadosExperimentoToAttach.getDadosExperimentoPK());
                attachedDadosExperimentoCollectionNew.add(dadosExperimentoCollectionNewDadosExperimentoToAttach);
            }
            dadosExperimentoCollectionNew = attachedDadosExperimentoCollectionNew;
            experimento.setDadosExperimentoCollection(dadosExperimentoCollectionNew);
            Collection<Variavel> attachedVariavelCollectionNew = new ArrayList<Variavel>();
            for (Variavel variavelCollectionNewVariavelToAttach : variavelCollectionNew) {
                variavelCollectionNewVariavelToAttach = em.getReference(variavelCollectionNewVariavelToAttach.getClass(), variavelCollectionNewVariavelToAttach.getIdvariavel());
                attachedVariavelCollectionNew.add(variavelCollectionNewVariavelToAttach);
            }
            variavelCollectionNew = attachedVariavelCollectionNew;
            experimento.setVariavelCollection(variavelCollectionNew);
            experimento = em.merge(experimento);
            if (idusuarioOld != null && !idusuarioOld.equals(idusuarioNew)) {
                idusuarioOld.getExperimentoCollection().remove(experimento);
                idusuarioOld = em.merge(idusuarioOld);
            }
            if (idusuarioNew != null && !idusuarioNew.equals(idusuarioOld)) {
                idusuarioNew.getExperimentoCollection().add(experimento);
                idusuarioNew = em.merge(idusuarioNew);
            }
            for (Amostra amostraCollectionNewAmostra : amostraCollectionNew) {
                if (!amostraCollectionOld.contains(amostraCollectionNewAmostra)) {
                    Experimento oldIdexperimentoOfAmostraCollectionNewAmostra = amostraCollectionNewAmostra.getIdexperimento();
                    amostraCollectionNewAmostra.setIdexperimento(experimento);
                    amostraCollectionNewAmostra = em.merge(amostraCollectionNewAmostra);
                    if (oldIdexperimentoOfAmostraCollectionNewAmostra != null && !oldIdexperimentoOfAmostraCollectionNewAmostra.equals(experimento)) {
                        oldIdexperimentoOfAmostraCollectionNewAmostra.getAmostraCollection().remove(amostraCollectionNewAmostra);
                        oldIdexperimentoOfAmostraCollectionNewAmostra = em.merge(oldIdexperimentoOfAmostraCollectionNewAmostra);
                    }
                }
            }
            for (DadosExperimento dadosExperimentoCollectionNewDadosExperimento : dadosExperimentoCollectionNew) {
                if (!dadosExperimentoCollectionOld.contains(dadosExperimentoCollectionNewDadosExperimento)) {
                    Experimento oldExperimentoOfDadosExperimentoCollectionNewDadosExperimento = dadosExperimentoCollectionNewDadosExperimento.getExperimento();
                    dadosExperimentoCollectionNewDadosExperimento.setExperimento(experimento);
                    dadosExperimentoCollectionNewDadosExperimento = em.merge(dadosExperimentoCollectionNewDadosExperimento);
                    if (oldExperimentoOfDadosExperimentoCollectionNewDadosExperimento != null && !oldExperimentoOfDadosExperimentoCollectionNewDadosExperimento.equals(experimento)) {
                        oldExperimentoOfDadosExperimentoCollectionNewDadosExperimento.getDadosExperimentoCollection().remove(dadosExperimentoCollectionNewDadosExperimento);
                        oldExperimentoOfDadosExperimentoCollectionNewDadosExperimento = em.merge(oldExperimentoOfDadosExperimentoCollectionNewDadosExperimento);
                    }
                }
            }
            for (Variavel variavelCollectionNewVariavel : variavelCollectionNew) {
                if (!variavelCollectionOld.contains(variavelCollectionNewVariavel)) {
                    Experimento oldIdexperimentoOfVariavelCollectionNewVariavel = variavelCollectionNewVariavel.getIdexperimento();
                    variavelCollectionNewVariavel.setIdexperimento(experimento);
                    variavelCollectionNewVariavel = em.merge(variavelCollectionNewVariavel);
                    if (oldIdexperimentoOfVariavelCollectionNewVariavel != null && !oldIdexperimentoOfVariavelCollectionNewVariavel.equals(experimento)) {
                        oldIdexperimentoOfVariavelCollectionNewVariavel.getVariavelCollection().remove(variavelCollectionNewVariavel);
                        oldIdexperimentoOfVariavelCollectionNewVariavel = em.merge(oldIdexperimentoOfVariavelCollectionNewVariavel);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = experimento.getIdexperimento();
                if (findExperimento(id) == null) {
                    throw new NonexistentEntityException("The experimento with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Experimento experimento;
            try {
                experimento = em.getReference(Experimento.class, id);
                experimento.getIdexperimento();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The experimento with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Amostra> amostraCollectionOrphanCheck = experimento.getAmostraCollection();
            for (Amostra amostraCollectionOrphanCheckAmostra : amostraCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Experimento (" + experimento + ") cannot be destroyed since the Amostra " + amostraCollectionOrphanCheckAmostra + " in its amostraCollection field has a non-nullable idexperimento field.");
            }
            Collection<DadosExperimento> dadosExperimentoCollectionOrphanCheck = experimento.getDadosExperimentoCollection();
            for (DadosExperimento dadosExperimentoCollectionOrphanCheckDadosExperimento : dadosExperimentoCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Experimento (" + experimento + ") cannot be destroyed since the DadosExperimento " + dadosExperimentoCollectionOrphanCheckDadosExperimento + " in its dadosExperimentoCollection field has a non-nullable experimento field.");
            }
            Collection<Variavel> variavelCollectionOrphanCheck = experimento.getVariavelCollection();
            for (Variavel variavelCollectionOrphanCheckVariavel : variavelCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Experimento (" + experimento + ") cannot be destroyed since the Variavel " + variavelCollectionOrphanCheckVariavel + " in its variavelCollection field has a non-nullable idexperimento field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Usuario idusuario = experimento.getIdusuario();
            if (idusuario != null) {
                idusuario.getExperimentoCollection().remove(experimento);
                idusuario = em.merge(idusuario);
            }
            em.remove(experimento);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Experimento> findExperimentoEntities() {
        return findExperimentoEntities(true, -1, -1);
    }

    public List<Experimento> findExperimentoEntities(int maxResults, int firstResult) {
        return findExperimentoEntities(false, maxResults, firstResult);
    }

    private List<Experimento> findExperimentoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Experimento as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Experimento findExperimento(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Experimento.class, id);
        } finally {
            em.close();
        }
    }

    public int getExperimentoCount() {
        EntityManager em = getEntityManager();
        try {
            return ((Long) em.createQuery("select count(o) from Experimento as o").getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
